<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.bbb.*"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String studentId = "0";
if(StringUtils.isNotBlank(request.getParameter("student_id"))){
	studentId = request.getParameter("student_id");
}
String dual = "0";
if(StringUtils.isNotBlank(request.getParameter("chosen"))){
	dual = request.getParameter("chosen");
}
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String brithday = request.getParameter("brithday");
String address = request.getParameter("address");
String maddress = request.getParameter("maddress");
String maddressarea = request.getParameter("maddressarea");
String id = request.getParameter("idnum");
String mailaddress = maddress + "@" + maddressarea;
StudentSet ss = new StudentSet();
List<Map<String,String>> list = null;
Map<String,String> mmap = null;


if(dual.equals("3"))
{
	list = ss.selectInfo(name,sex,address,maddress,maddressarea,brithday);
}else
{
	if(dual.equals("1")){
		ss.insertStudent(name,sex,address,mailaddress,brithday);
	}else if(dual.equals("2")){
		ss.changeDate(id,name,sex,address,mailaddress,brithday);
	}else if(dual.equals("4")){
		mmap = ss.makesureDate(studentId);
	}else if(dual.equals("5")){
		ss.deleteDate(studentId);
	}
	list =	ss.selectDate();
}
	%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script>
		function duel(it)
		{
			chosen.value=it;
			form_main.submit();
		}
	</script>
  </head>
  
  <body>
    <form id="form_main" action="index.jsp" method="post">
    <div>
	    <span>学生姓名</span>
	    <%
	    String name_box = "";
	    if(dual.equals("4")){
	    	name_box = mmap.get("name");
	    }
	    String namebox = "<input name='name' id='name' type='text' value='"+name_box+"'/>";
	    out.print(namebox);
	     %>
	    	
		<span>性别</span>
		<%
		String sex_chose = "";
		if(dual.equals("4")){
			sex_chose = mmap.get("sex");
		}
		String sex_male = "<input name='sex' id='sex' type='radio' value='男'";
		if(sex_chose.equals("男")){
			sex_male = sex_male + " checked='default' ";
		}
		sex_male = sex_male + "/>男";
		out.print(sex_male);
		String sex_female = "<input name='sex' id='sex' type='radio' value='女'";
		if(sex_chose.equals("女")){
			sex_female = sex_female + " checked='default' ";
		}
		sex_female = sex_female + "/>女";
		out.print(sex_female);
		String sex_unknow = "<input name='sex' id='sex' type='radio' value='不明'";
		if(sex_chose.equals("不明")){
			sex_unknow = sex_unknow + " checked='default' ";
		}
		sex_unknow = sex_unknow + "/>不明";
		out.print(sex_unknow);
		 %>
	    <br/>
	    <span>生日</span>
	    <%
	    String brithday_box = "";
	    if(dual.equals("4")){
	    	brithday_box = mmap.get("brithday");
	    }
	    String brithdaybox = "<input name='brithday' id='brithday' type='text' value='" + brithday_box + "'/>";
	    out.print(brithdaybox);
	     %>
	    <span>邮箱</span>
	    <%
	    String maddress_box = "";
	    String maddressarea_box = "";
	    if(dual.equals("4")){
	    	maddress_box = mmap.get("maddress");
	    	maddressarea_box = mmap.get("maddressarea");
	    }
	    String maddressbox = "<input name='maddress' id='maddress' type='text' value='" + maddress_box + "'/>@";
	    String m189_selected = "",sina_selected = "",qq_selected = "";
	    if(maddressarea_box.equals("qq.com")){
	    	qq_selected = "selected";
	    }else if(maddressarea_box.equals("sina.com")){
	    	sina_selected = "selected";
	    }else if(maddressarea_box.equals("189.cn")){
	    	m189_selected = "selected";
	    }
	    String maddressareabox = "<select name='maddressarea' id='maddressarea' size='1'><option value=''></option><option value='sina.com' " + sina_selected + ">sina.com</option><option value='qq.com' " + qq_selected +">qq.com</option><option value='189.cn' " + m189_selected + ">189.cn</option></select>";
	    out.print(maddressbox);
	    out.print(maddressareabox);
	     %>
	    <br/>
	    <span>地址</span>
	    <%
	    String address_box = "";
	    if(dual.equals("4")){
	    	address_box = mmap.get("address");
	    }
	    String addressbox = "<textarea name='address' id='address' rows='2' cols='50'>" + address_box + "</textarea>";
	    out.print(addressbox);
	     %><br/>
			<input value="登陆" onclick="duel(1)" type="button"/>
			<input value="保存" onclick="duel(2)" type="button"/>
			<input value="查询" onclick="duel(3)" type="button"/>
    </div>
    <input type="hidden" name="chosen" id="chosen" value="0"/>
    <%
    String hidden_id = "<input type='hidden' name='idnum' id='idnum' value='"+studentId+"'/>";
    out.print(hidden_id);
     %>
    <br/>
    <table border=1 width="500">
    	<tr>
    		<td>学生姓名</td>
    		<td>性别</td>
    		<td>生日</td>
    		<td>邮箱</td>
    		<td>修改</td>
    		<td>删除</td>
    	</tr>
    	<%
    	for(int i = 0; list != null && i <list.size();i++){
    	 Map<String,String> map = list.get(i);
    	 %>
    	<tr>
    		<td><% out.print(map.get("name")); %></td>
    		<td><% out.print(map.get("sex")); %></td>
    		<td><% out.print(map.get("brithday")); %></td>
    		<td><% out.print(map.get("maddress")); %></td>
    		<td>
	    		<%
	    		String updatelink = "<a href='index.jsp?student_id="+map.get("tb_student_id")+"&chosen=4"+"'>修改</a>";
	    		out.print(updatelink);
	    		 %>
    		</td>
    		<td>
	    		<%
	    		String deletelink = "<a href='index.jsp?student_id="+map.get("tb_student_id")+"&chosen=5"+"'>删除</a>";
	    		out.print(deletelink);
	    		 %>
    		</td>
    	</tr>
    	<% }%>
     </table>
    </form>
  </body>
</html>
